﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Book_of_Shadow_Script.Scripts
{
    class BookofShadowDetail : ScriptDetail
    {
        public BookofShadowDetail(long offset, int order, CustomEncoding en = null)
            : base(offset, order, en)
        { }


        public override int len()
        {
            //if ((buf[1] & (Byte)1)==1) System.Windows.Forms.MessageBox.Show(String.Format("{0:X}", offset));
            if (buf[1] == 0)
                return 12;
            else
                if (buf[1] == 0x90)
                    return 2;
                else
                    return buf[1] - (buf[1] & 1);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            String type="";
            sb.AppendFormat("{0:X6}: {1:X4} ", base.Offset,base.Order);
            switch (ScpType())
            {
                case 0x01: type = this.text; break;//直接跳转
                case 0x06: type = this.text; break;//判断跳转
                case 0x08: type = "选择支跳转"; break;//选择支跳转
                case 0x18: type = this.text; break;//文本
                case 0x1A: type = "消除对话框"; break;
                case 0x1B: type = "选择支"; break;
                case 0x1E: type = "【" + this.text + "】"; 
                    break;//中间提示文本
                case 0x19:
                case 0x1C:
                    System.Windows.Forms.MessageBox.Show(string.Format("{0:X}", base.Offset));
                    break;//中间提示文本
                case 0x1F: type =  this.text;
                    //System.Windows.Forms.MessageBox.Show(string.Format("{0:X} {1}",offset,this.text));
                    break;//人名提示文本
                case 0x20: type = "对话框"; break;
                case 0x42: type = "立绘"; break;
                case 0x43: type = "消立绘"; break;
                case 0x90: type = "背景1/5"; break;
                case 0x91: type = "背景5/5"; break;
                case 0x94: type = "背景4/5"; break;
                case 0x96: type = "背景3/5"; break;
                case 0x9C: type = "背景2/5"; break;
                case 0xC8: type = "音效1"; break;
                case 0xC9: type = "音效2"; break;
            }
            if (type != "")
            {
                sb.Append("  ");
                sb.Append(type);
            }
            return sb.ToString();
        }
        
        public override string DisplayByte()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{1:X2}{0:X2} ", buf[0], buf[1]);
            for (int i = 2; i < len(); i += 2)
                sb.AppendFormat("{1:X2}{0:X2} ", buf[i], buf[i + 1]);
            return sb.ToString();
        }
        public override int ScpType()
        {
            return buf[0];
        }

        public override void analyze(FileStream fs)
        {
            if (ScpType() == 0x18)//文本
            {
                long oft = Byte2Int(6, 4);
                Int32 len = 0;
                this.text = ReadFileString(fs, oft, ref len);
                outtexts.Add(new OutText(oft, len, text));
            }
            if (ScpType() == 0x1E)//中间提示文本
            {
                long oft = Byte2Int(2, 4);
                Int32 len = 0;
                this.text = ReadFileString(fs, oft, ref len);
                outtexts.Add(new OutText(oft, len, text));
            }
            if (ScpType() == 0x1F)//人名提示文本
            {
                long oft = Byte2Int(6, 4);
                Int32 len = 0;
                this.text = ReadFileString(fs, oft, ref len);
                outtexts.Add(new OutText(oft, len, text));
            }
            if (ScpType() == 0x01)//跳转
            {
                long pos = fs.Position;
                long oft = Byte2Int(2, 4);
                this.text = String.Format("跳转至0x{0:X6}", oft);
            }
            if (ScpType() == 0x06)//判断跳转
            {
                long pos = fs.Position;
                long oft = Byte2Int(2, 4);
                this.text = String.Format("判断跳转至0x{0:X6}", oft);
            }
            if (ScpType() == 0x1B)//选择支
            {
                long pos = fs.Position;
                long cnt = Byte2Int(2, 2);
                StringBuilder sb = new StringBuilder();
                sb.Append("选择支：");
                for (int i = 0; i < cnt; i++)
                {
                    long oft = Byte2Int(i * 10 + 8, 4);
                    Int32 len = 0;
                    String tt;
                    sb.Append(Environment.NewLine);
                    sb.AppendFormat("{0:D}---", i);
                    tt = ReadFileString(fs, oft, ref len);
                    sb.Append(tt);
                    outtexts.Add(new OutText(oft, len, tt));
                }
                this.text = sb.ToString();
            }
            if (ScpType() == 0x08)//选择支跳转
            {
                long pos = fs.Position;
                int cnt = Byte2Int(4, 2);
                StringBuilder sb = new StringBuilder();
                sb.Append("选译支跳转：");
                for (int i = 0; i < cnt; i++)
                {
                    long oft = Byte2Int(i * 6 + 8, 4);
                    sb.Append(Environment.NewLine);
                    sb.AppendFormat("{0:D}--- ", i);
                    sb.AppendFormat("{0:X6}\n", oft);
                }
                this.text = sb.ToString();
            }
        }
        public override String TextOut()
        {
            StringBuilder sb = new StringBuilder();
            if (hasText())
            {
                sb.AppendFormat("{0:X},", base.Offset);
                sb.AppendFormat("{0:X},", ScpType());
                sb.AppendFormat("{0:D}", outtexts.Count);
                sb.Append(Environment.NewLine);
                foreach (OutText ot in outtexts)
                {
                    sb.AppendFormat("{0:X},", ot.offset);
                    sb.AppendFormat("{0:D}", ot.len);
                    sb.Append(Environment.NewLine);
                    sb.Append("原文--");
                    sb.Append(ot.text);
                    sb.Append(Environment.NewLine);
                    sb.Append("翻译--");
                    sb.Append(Environment.NewLine);
                    sb.Append("润色--");
                    sb.Append(Environment.NewLine);
                    sb.Append("注释--");
                    sb.Append(Environment.NewLine);
                }

            }
            return sb.ToString();
        }

        public override bool hasText()
        {
            return this.ScpType() == 0x18 || this.ScpType() == 0x1B || this.ScpType() == 0x1E || this.ScpType() == 0x1F;
        }
        public override void ReadHead(FileStream fs)
        {
            Byte[] hd = new Byte[2];
            fs.Read(hd, 0, 2);
            if (hd[1] != 0)
                fs.Seek(0x20, SeekOrigin.Begin);
            else
                fs.Seek(hd[0], SeekOrigin.Begin);
        }
    }
}
